﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tesseract;

namespace OCR
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string txt = "";

            string defaultList = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            const string language = "zgyd";

            //Nuget安装的Tessract版本为3.02，tessdata的版本必须与其匹配，另外路径最后必须以"\"或者"/"结尾
             string TessractData = this.tbFolder.Text;

            Tesseract.TesseractEngine test = new Tesseract.TesseractEngine(TessractData, language);

            test.SetVariable("tessedit_char_whitelist", defaultList);

            Bitmap tmpVal = new Bitmap(this.tbPic.Text);

            //Bitmap tmpBmp = Preprocess.Preprocess.Sauvola(tmpVal);

            //pictureBox2.Image = (Image)tmpBmp;

            Page tmpPage = test.Process(tmpVal,test.DefaultPageSegMode);

            this.textBox1.Text = tmpPage.GetText();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.Filter = "所有文件(*.*)|*.*|图片(*.bmp)|*.bmp";
            if(this.openFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
            {
                this.tbPic.Text = this.openFileDialog1.FileName;
                this.pictureBox1.Image = Image.FromFile(this.tbPic.Text);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (this.folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.tbFolder.Text = this.folderBrowserDialog1.SelectedPath+"\\";
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.tbFolder.Text = @"D:\Lingxin.Card\OCR\tessdata\";
        }

        private void btnFileOpen_Click(object sender, EventArgs e)
        {

        }

        private void btnPicProcess_Click(object sender, EventArgs e)
        {
             Bitmap tmpVal = new Bitmap(this.tbPic.Text);
             PictureHelper.ToGray(tmpVal).Save(this.tbPic.Text.Replace(".png", "_gray.png"));

            
        }

        private void btnTwo_Click(object sender, EventArgs e)
        {
            Bitmap tmpVal = new Bitmap(this.tbPic.Text);
            PictureHelper.ConvertTo1Bpp2(tmpVal).Save(this.tbPic.Text.Replace(".png", "_two.png"));
        }
    }
}
